/*
 * Copyright (c) 2016. André Mion
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.sample.andremion.musicplayer.activities;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.transition.Transition;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

import com.sample.andremion.musicplayer.R;
import com.sample.andremion.musicplayer.music.MusicContent;
import com.sample.andremion.musicplayer.music.MusicControl;
import com.sample.andremion.musicplayer.utils.MediaUtil;
import com.sample.andremion.musicplayer.view.CoverView;
import com.sample.andremion.musicplayer.view.TransitionAdapter;

public class DetailActivity extends PlayerActivity {

    private CoverView mCoverView;
    private TextView title;
    private FloatingActionButton fabbutton;
    private ImageView radndom;
    private ImageView sunxu;

    private boolean noExit = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_detail);

        fabbutton = (FloatingActionButton) findViewById(R.id.fab);
        mCoverView = (CoverView) findViewById(R.id.cover);
        title = (TextView) findViewById(R.id.title);
        radndom = (ImageView) findViewById(R.id.shuffle);
        sunxu = (ImageView) findViewById(R.id.sunxu);

        radndom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setPlayMode(MusicControl.MODE_RANDOM);
            }
        });

        sunxu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setPlayMode(MusicControl.MODE_SUNXU);
            }
        });

        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCoverView.stop();
            }
        });

        View next = findViewById(R.id.next);

        MusicContent.MusicItem item = MusicControl.getCurrentItem();
        Bitmap artwork = MediaUtil.getArtwork(getBaseContext(), item.mId, item.mAlbumId, true, false);
        mCoverView.setImageBitmap(artwork);

        title.setText(item.mTitle + "-" + item.mArtist);

        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MusicControl.nextMusic();
            }
        });

        mCoverView.setCallbacks(new CoverView.Callbacks() {
            @Override
            public void onStopAnimation() {
                if (!noExit)
                    supportFinishAfterTransition();
                noExit = false;
            }
        });

        int playMode = MusicControl.getPlayMode();
        if (playMode == MusicControl.MODE_RANDOM) {
        }

        getWindow().getSharedElementEnterTransition().addListener(new TransitionAdapter() {
            @Override
            public void onTransitionEnd(Transition transition) {
                if (!MusicControl.isPlay()) {
                    MusicControl.playStop();
                }
                mCoverView.start();
            }
        });

        setPlayMode(MusicControl.getPlayMode());
    }

    private void setPlayMode(int mode) {
        if (mode == MusicControl.MODE_RANDOM) {
            MusicControl.setPlayMode(MusicControl.MODE_RANDOM);
            sunxu.getDrawable().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);
            radndom.getDrawable().setColorFilter(getResources().getColor(R.color.colorAccent), PorterDuff.Mode.SRC_IN);
        } else if (mode == MusicControl.MODE_SUNXU) {
            MusicControl.setPlayMode(MusicControl.MODE_SUNXU);
            radndom.getDrawable().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);
            sunxu.getDrawable().setColorFilter(getResources().getColor(R.color.colorAccent), PorterDuff.Mode.SRC_IN);
        }
    }

    private MusicControl.OnMusicChangeListener musicChangeListener = new MusicControl.OnMusicChangeListener() {
        @Override
        public void onPlayStatusChange() {
            if (MusicControl.isPlay()) {
                fabbutton.setImageResource(R.drawable.ic_pause_animatable);
            } else {
                noExit = true;
                mCoverView.stop();
                fabbutton.setImageResource(R.drawable.ic_play_animatable);
            }
        }

        @Override
        public void onChange(MusicContent.MusicItem item) {
            title.setText(item.mTitle + "-" + item.mArtist);
            Bitmap artwork = MediaUtil.getArtwork(getBaseContext(), item.mId, item.mAlbumId, true, false);
            mCoverView.setImageBitmap(artwork);
            mCoverView.setAlpha(0.2f);
            mCoverView.animate().alpha(1f).setInterpolator(new AccelerateInterpolator()).start();
            onUpdateProgress();
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        MusicControl.setMusicChangeListener(musicChangeListener);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MusicControl.removeMusicChangeListener(musicChangeListener);
    }

    @Override
    public void onBackPressed() {
        mCoverView.stop();
    }

    public void onFabClick(View view) {
        if (!MusicControl.isPlay()) {
            fabbutton.setImageResource(R.drawable.ic_pause_animatable);
            mCoverView.start();
            MusicControl.playStop();
            return;
        }
        MusicControl.playStop();
        mCoverView.stop();
    }

}
